package com.dongdongqiang.admin.config;

import com.alibaba.druid.filter.Filter;
import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.util.JdbcUtils;
import com.alibaba.druid.wall.WallConfig;
import com.alibaba.druid.wall.WallFilter;
import com.dongdongqiang.common.cache.CacheComponent;
import com.dongdongqiang.common.query.GenericQueryDAO;
import com.dongdongqiang.common.query.mysql.MysqlGenericQueryDAO;
import com.dongdongqiang.common.query.postgres.PostgresGenericQueryDAO;
import com.dongdongqiang.daogenerator.DDQDAO;
import com.dongdongqiang.daogenerator.config.DDQDAOConfig;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.web.servlet.ServletComponentScan;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import org.springframework.util.ResourceUtils;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

import javax.sql.DataSource;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * 管理后台Controller
 * Created by ZZB on 2017/9/18.
 */
@Configuration
@ServletComponentScan("com.dongdongqiang.admin")
@ComponentScan({"com.dongdongqiang.admin"})
@EnableTransactionManagement
@MapperScan("com.dongdongqiang.admin.dao")
public class AdminAppConfig extends WebMvcConfigurerAdapter {
    @Value("${spring.datasource.url}")
    private String url;
    @Value("${spring.datasource.username}")
    private String username;
    @Value("${spring.datasource.password}")
    private String password;
    @Value("${spring.database.type:postgresql}")
    private String databaseType;

    @Value("${spring.datasource.driver-class-name}")
    private String driverClassName;
    @Value("${spring.datasource.filters}")
    private String filters;
    @Value("${spring.mybatis.mapper.location:classpath*:postgresql-mapper/**/*.xml}")
    private String mapperLocation;

    @Bean(name = "dataSource")
    DataSource dataSource() {
        try {
            DruidDataSource dataSource = new DruidDataSource();
            dataSource.setDriverClassName(driverClassName);
            dataSource.setUrl(url);
            dataSource.setUsername(username);
            dataSource.setPassword(password);
            dataSource.setFilters(filters);
            List<Filter> proxyFilters = new ArrayList<>();
            proxyFilters.add(wallFilter());
            dataSource.setProxyFilters(proxyFilters);
            return dataSource;
        } catch (SQLException e) {
            e.printStackTrace();
            return null;
        }
    }

    @Bean
    WallFilter wallFilter() {
        WallFilter wallFilter = new WallFilter();
        wallFilter.setDbType("mysql");
        wallFilter.setConfig(wallConfig());
        return wallFilter;
    }

    @Bean
    WallConfig wallConfig() {
        WallConfig wallConfig = new WallConfig();
        wallConfig.setConditionAndAlwayTrueAllow(true);
        wallConfig.setConditionAndAlwayFalseAllow(true);
        return wallConfig;
    }

    @Bean
    public SqlSessionFactory sqlSessionFactory() throws Exception {
        SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean();
        sessionFactory.setDataSource(dataSource());
        PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
        sessionFactory.setMapperLocations(resolver
                .getResources(mapperLocation))
        ;

        return sessionFactory.getObject();
    }

    @Bean
    GenericQueryDAO genericQueryDAO() {
        if (JdbcUtils.MYSQL.equals(databaseType)) return new MysqlGenericQueryDAO();
        if (JdbcUtils.POSTGRESQL.equals(databaseType)) return new PostgresGenericQueryDAO();
        return new MysqlGenericQueryDAO();
    }

    @Bean
    DDQDAO ddqdao(DataSource dataSource) {
        DDQDAOConfig ddqdaoConfig = new DDQDAOConfig();
        ddqdaoConfig.setDataSource(dataSource);
        ddqdaoConfig.setJdbcType(databaseType);
        return new DDQDAO(ddqdaoConfig);
    }

    @Bean
    public CacheComponent cacheComponent(){
        return new CacheComponent();
    }

//    @Override
//    public void addResourceHandlers(ResourceHandlerRegistry registry) {
//        registry.addResourceHandler("/templates/**").addResourceLocations(ResourceUtils.CLASSPATH_URL_PREFIX+"/templates/");
//        registry.addResourceHandler("/**").addResourceLocations(ResourceUtils.CLASSPATH_URL_PREFIX+"/static/");
//        registry.addResourceHandler("/css/**").addResourceLocations(ResourceUtils.CLASSPATH_URL_PREFIX+"/static/css/");
//        registry.addResourceHandler("/img/**").addResourceLocations("file:H:\\program\\java\\dongdongqiang\\dongdongqiang-admin\\src\\main\\resources\\static\\img"); //可以直接指定文件路径
//        super.addResourceHandlers(registry);
//    }

/*
    @Bean
    public SysUserLoginInterceptor sysUserLoginInterceptor(){
        return new SysUserLoginInterceptor();
    }

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(sysUserLoginInterceptor());
    }
*/

}
